/**
*@function search-ui.js 
*@author   kevin peng
*@date     2010-9-1
*/
(function(){
	function sortNumber(a, b){return a - b;}
	function SearchText(){};
	SearchText.prototype = {
	    //generate the list content
		_getListCt: function(){
			var self = this , fl = 'list-f', ls = "<div class='search'>", data = this._serverCity || this.localCity;
			self.popCt.addClass('search-list'); 
			if(self._serverCity && self._serverCity.length > 0){
			   fl = 'list-l';
			}else{
				if($.trim(self.input.val()) && self.isSearch){
					ls += ['<div class="n-f">对不起，找不到: ',self.input.val(),'</div>'].join('');
				}else{
					ls += ["<div class='search title'><div class='search'>",self.title,"</div><a class='close'></a></div>"].join('');
				}
			}
			if(data && data.length > 0){
				ls += "<ul>";
				$.each(data,function(){
					ls += ["<li class=",fl," t=",this.text," p='",this.provinceCode,"'c='",this.cityCode,"'dpt='",this.dpt,"'pName='",this.pName,
					"'cName='",this.cName,"'carNo='",this.carNo,"'><span><a href='javascript:void(0);'>",this.text,"</a></span>","<span><a class='list-a' style='display:",
					fl == 'list-l' ? 'inline':'none',"' href='javascript:void(0);'>",this['alias'],'</a></span>',"</li>"].join('')
				})	
				ls += '</ul>';
			}				
			return ls + '</div>';
		},
		//bind the click event
		_listClick: function(){
			var self = this;
			$('.search-list li',this.popCt).live('click',function(e){
				var t = e.target;
				while(t!= this && t.parentNode) t = t.parentNode;
				self.setValue($(t).attr('t'),[$(t).attr('p'),$(t).attr('c'),$(t).attr('pName'),$(t).attr('cName'),$(t).attr('dpt'),$(t).attr('carNo')].join('|'));				
				self._triggerEvent('select',self.hiddenField.val(),e);
				self.close();
				return false;
			})
			$(document).bind('click',function(e){
				if(!$(e.target).hasClass('search') && self.isShow){
				  self.close();
				  //self.input.focus();
				}
			}).bind('keydown',function(e){
				var keyCode = e.keyCode , items = self.popCt.find('li');
				if(keyCode == 229) self.isEnterKey = true;
				if(items.hasClass('list-l')){ --keyCode;}
				if(self.isShow && items.size() > 0){ 
					var it = items.filter('.li-hover'), li = ''; it.removeClass('li-hover'), inx = items.index(it);
					if(keyCode == 38){
					   li = (inx - 4) >= 0 ? items.eq(inx - 4) : it;
					   if(inx == 0) li = it.siblings(':last');
					}else if (keyCode == 40){
					   li = (inx + 4) <= 19 ? items.eq(inx + 4) : it;
					   if(inx == 19) li = it.siblings(':first')
					}else if(keyCode == 13){
					   if(it.size() > 0) it.click();
					}else if(keyCode == 37){
					   li = it.prev().size() > 0 ? it.prev() : it.siblings(':last');
					}else if(keyCode == 39){
					   li = it.next().size() > 0 ? it.next() : items.eq(0);
					}else if((keyCode == 12 || keyCode == 27) && self.isShow){
						if(keyCode == 12) it.click();
						self.close();
					}
					if(li && li.length > 0) li.addClass('li-hover');
				}else{
				   if(self.hiddenField.val() && (keyCode == 12 || keyCode == 13)){
				      self._triggerEvent('focus',e);
				   }
				}
			})
		},    
		//reset ths list content
		_rebulidPopCt: function(){
		    this.popCt.empty().width(this.listWidth).append(this._getListCt()).hide();
			$('li',this.popCt).hover(function(){
			     $(this).siblings().removeClass('li-hover')
				 $(this).addClass('li-hover')
			  },function(){
			      //$(this).removeClass('li-hover')
			  });
		},	
		//display the list content
		_show: function(){
			var ps = this.input;
			this.popCt.css({position: 'absolute',left: (ps.position().left - 1) + 'px',top: ps.position().top + ps.outerHeight() + 'px'})
			.css('z-index', 9999999).show();
			$('li:first',this.popCt).addClass('li-hover');
			if(!this.isShow) this._triggerEvent('drop');
			var h = $('li:first',this.popCt).innerHeight();
			if(!this._serverCity){
			  $('ul',this.popCt).height(h*6);
			}else{
			  var isIE = $.browser.msie;
              var isIE6 = isIE && !window.XMLHttpRequest;
			  var isIE8 = isIE && !!document.documentMode;
              var isIE7 = isIE && !isIE6 && !isIE8;
			  $('ul',this.popCt).height(h*((this._serverCity.length)+1));			 
			  //if(isIE6 || isIE7) $('li',this.popCt).height(h - 5);			  
			}
			this.isShow = true;
		},
		//close pop content
		close: function(){
			this.popCt.hide();
			this.isShow = false;
			this._triggerEvent('undrop');
		},
		//bind search event
		_searchText: function(){
			var self = this;
			this.input.bind('keyup',function(e){
			  var keyCode = e.keyCode; 
			  self.isSearch = true;
			  if((keyCode >= 65 && keyCode <= 90)||(keyCode >= 48 && keyCode <= 57)||(keyCode >= 96 && keyCode <= 105)
			  || keyCode == 8 || keyCode == 32 || self.isEnterKey){
				 self.timeout = setTimeout(function(){
				    self.hiddenField.val('');
					self._triggerEvent('editor',self.input.val(),self.hiddenField,e);
					self._filterText(); 
					self._show();
					self.isEnterKey = false;
				 },300);
			  }else if($.inArray(keyCode,[37,38,39,40]) == -1){
				self.close()
			  }
			});
		},
        //input search text
		_filterText: function(txt){
		   var self = this , str = $.trim(txt || self.input.val()), ls = [] , ex = [];
		   if(str = str.toLowerCase().replace(/\\/g,'_')){
			  $(this.serverCity).each(function(i,n){
				  if(ls.length < 8){
				      var s = n.split(',') , isCn = /^[\u4e00-\u9fa5]/.test(str);
					  if(s[isCn ? 3 : 5].search(new RegExp("^" + str,'i')) > -1){
						 /0{2}$/.test(s[2]) ? ls.unshift(n) : ex.push(n);
					  }else if(s[isCn ? 4 : 6].search(new RegExp("^" + str,'i')) > -1){
						/0{2}$/.test(s[2]) ? ls.unshift(n) : ex.push(n);
					  }
				  }
		      });
			  if(ls.length < 8) ls = ls.concat(ex.slice(0,8 - ls.length));
		   }
		   self._serverCity = (ls.length > 0 || str) ? self._filterData(ls) : '';		   
		   self._rebulidPopCt();
		},
		//search result
        _filterData: function(ls){
			var ls_data = [] , ix = 1000;
			$(ls).each(function(n,i){
			  var d = this.split(',');
			  if(d[4].indexOf('null') == -1){ 
			    var inx = Number(d[9] || ix++);
				ls_data.push({provinceCode:d[0],cityCode:d[2],text:d[4],pName:d[1],cName:d[3],alias:d[5],s:d[6],dpt:d[7],carNo:d[8],inx:inx})
			  }
			})
		    return ls_data.sort(function(a,b){return a.inx > b.inx ? 1 : -1;});
		 },
		 //trigger eventhandler
        _triggerEvent: function(type,opt,e){
			var fn = this.opt[type];
			if(fn && $.isFunction(fn)) fn.apply(this,[opt,e]);
		},			
		//rebuild service data format
		_splitData: function(data){
			var self = this; this.citys = []; 
			$(data.replace(/(^\|*)|(\|*$)/g,'').split('||'))
			.each(function(i,n){
				n.indexOf('|') > -1 ? self._splitCity(n) : self.citys.push(n);
			})
			return self.citys;
		},		
		//bulid data string
		_splitCity: function(c){
			var self = this , ct = c.split('|'), p = ct[0].split(',');
		    $(ct).each(function(i,n){
				self.citys.push(i > 0 ? ((p.slice(0,2).concat([n])).join(',')) : n)
			})
		},	
		// bind input click event
		_inputClick: function(){
		   var obj = this;
		   obj.input.bind('click',function(){
				if($(this).hasClass('search-text')) $(this).removeClass('search-text').val('');
				obj._serverCity = ''; obj.isSearch = false; obj._rebulidPopCt();
				//$(this).val() ? obj._filterText() : obj._rebulidPopCt();
				if(this.createTextRange){
				   var r = this.createTextRange() , sel = r.duplicate();     
				   sel.moveStart("character", $(this).val().length);     
				   sel.setEndPoint("EndToStart", r);     
				   sel.select();
				}else{
				   this.setSelectionRange(0,$(this).val().length);  
				   this.focus();  
				}
				obj.isShow ? obj.close(): obj._show();	
			}).blur(function(e){
			   var li = $('.li-hover',obj.popCt);
			   if(li && li.length > 0 && li.hasClass('list-l')){
				  obj.setValue(li.attr('t'),[li.attr('p'),li.attr('c'),li.attr('pName'),li.attr('cName'),li.attr('dpt'),li.attr('carNo')].join('|'));
				  obj._triggerEvent('select',obj.hiddenField.val(),e);
			   }else{
				  obj.hiddenField.val();
			   }	
			}).val('');
		},
		//set hidden field value
		setValue: function(text,value){
			if(text && value){
			  this.hiddenField.val(value);
			  this.input.val(text);
			  var inp = (this.input)[0], length = this.input.val().length; 
			  if((inp.createTextRange)){				
				var txt = inp.createTextRange(); 
				txt.moveStart('character',length); 
				txt.collapse(true); 
				//txt.select();
			  }else{
				inp.setSelectionRange(length,length);  
				inp.focus();
				}
			}
		},
		//ip location address
		locateAddress: function(text){
		  if(text && text.indexOf('.') > -1){ 	
			  var ad = text.split('.'); this._filterText(ad[2]);
			  var code = this._serverCity;
			  if(code && code.length > 0){
				this.setValue(ad[2],[code[0].provinceCode,code[0].cityCode,code[0].pName,code[0].cName,code[0].dpt,code[0].carNo].join('|'));
				this._triggerEvent('select',this.hiddenField.val());
			  }else{
				this._serverCity = '';
				this.input.addClass('search-text').val(ad[ad.length-1])
			  }
		  }	
		}
	};
	//setting init config
	Search = {
		init: function(elem,options){
		    var opt = options || {} , self = elem , addr = opt.address, obj = new SearchText(); 
			obj.isShow = false; obj.listWidth = self.outerWidth() + (opt.expandWidth || 105);
			obj.localCity = opt.localCity; obj.serverCity = obj._splitData(opt.serverCity);	
			obj.title = opt.title || '&nbsp; 热门城市（其他城市请直接输入搜索）';
			obj.opt = opt; obj.popCt = $("<div>"); obj.input = self; 
			obj.hiddenField = $("<input type='hidden'>").attr('id',opt.hideName);
			obj._inputClick(); obj._listClick(); self.after(obj.hiddenField).after(obj.popCt); obj._searchText();  
			obj.locateAddress(addr);
		}
	};
})()